package initialize

import (
	"a_plus_station/api"
	"a_plus_station/middlewares"
	router2 "a_plus_station/router"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"net/http"
)

func getSessionSecret() string {
	secret := "djy666"
	return secret
}

// 主要路由
func Routers() *gin.Engine {
	// 获取一个基础的路由对象
	Router := gin.Default()

	// 1. 首先初始化会话中间件
	secret := getSessionSecret()
	store := cookie.NewStore([]byte(secret))

	// 配置会话选项
	store.Options(sessions.Options{
		Path:     "/",
		MaxAge:   3600 * 8,
		HttpOnly: true,
		Secure:   true, // 必须设置为 true（生产环境用 HTTPS）
		SameSite: http.SameSiteNoneMode,
	})

	// 添加会话中间件（必须在路由注册前添加）
	Router.Use(sessions.Sessions("oauth_session", store))

	// 2. 然后添加其他中间件
	Router.Use(middlewares.Cors()) // 确保这是您自定义的中间件
	// Router.Use(GinLogger(Logger), GinRecovery(Logger, true)) // 如果不需要可注释掉

	// 3. 最后注册路由
	ApiGroup := Router.Group("api/v1")
	ApiGroup.GET("healthy", api.Healthy)
	router2.InitBaseRouter(ApiGroup)
	router2.InitUserRouter(ApiGroup)
	router2.OAuth2Login(ApiGroup) // OAuth2 路由
	router2.InitGoodsRouter(ApiGroup)
	router2.InitCategoryRouter(ApiGroup)
	router2.InitOrderRouter(ApiGroup)

	return Router
}
